package com.alinu.workbench.service.impl;

import com.alinu.utils.CommonUtil;
import com.alinu.utils.SqlSessionUtil;
import com.alinu.vo.PaginationVO;
import com.alinu.workbench.domain.Activity;
import com.alinu.workbench.domain.ActivityRemark;
import com.alinu.workbench.mapper.ActivityMapper;
import com.alinu.workbench.mapper.ActivityRemarkMapper;
import com.alinu.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenzuwei
 * @date 2021/3/15
 * @description 市场活动业务实现类
 */
public class ActivityServiceImpl implements ActivityService {
    private final ActivityMapper activityMapper = SqlSessionUtil.getSqlSession().getMapper(ActivityMapper.class);

    private final ActivityRemarkMapper activityRemarkMapper = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkMapper.class);

    /**
     * 保存一个市场活动
     *
     * @param activity 市场活动
     */
    @Override
    public boolean saveActivity(Activity activity) {
        if (CommonUtil.isNotNull(activity)) {
            int count = activityMapper.saveActivity(activity);
            return count == 1;
        }
        return false;
    }

    /**
     * 获取市场活动列表
     *
     * @param params 参数
     * @return 统一输出数据对象
     */
    @Override
    public PaginationVO<Activity> getActivityList(Map<String, Object> params) {
        // 查询总条数
        int total = activityMapper.getTotal(params);

        // 查询活动列表
        List<Activity> activityList = activityMapper.getActivityList(params);

        PaginationVO<Activity> activityOutput = new PaginationVO<>();

        // 封装输出数据对象
        activityOutput.setDataList(activityList);

        activityOutput.setTotal(total);


        return activityOutput;
    }

    /**
     * 删除市场活动
     *
     * @param ids 要删除的活动id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteActivity(String[] ids) {
        boolean flag = true;
        Map<String, Object> params = new HashMap<>();

        params.put("ids", ids);
        params.put("isDelete", 1);
        // 查询需要删除的备注数量
        int count = activityRemarkMapper.getCount(params);

        // 实际删除的数量
        int deleteCount = activityRemarkMapper.deleteActivityRemark(params);

        if (count != deleteCount) {
            flag = false;
        }
        // 删除市场活动
        int delete = activityMapper.deleteActivity(params);

        if (delete != ids.length) {
            flag = false;
        }

        return flag;
    }

    /**
     * 查询某个市场活动
     *
     * @param id id
     * @return activity对象
     */
    @Override
    public Activity findActivity(String id) {

        return activityMapper.findActivity(id);

    }

    /**
     * 更新市场活动数据
     *
     * @param activity 对象
     * @return 是否更新成功
     */
    @Override
    public boolean updateActivity(Activity activity) {

        int count = activityMapper.updateActivity(activity);

        return count == 1;
    }

    /**
     * 查询市场活动备注列表
     *
     * @param activityId aid
     * @return 列表
     */
    @Override
    public List<ActivityRemark> getRemarkList(String activityId) {

        return activityRemarkMapper.getRemarkList(activityId);
    }

    @Override
    public List<Activity> findActivityByName(String name) {
        return activityMapper.findActivityByName(name);
    }

    /**
     * 获取市场活动通过活动名称
     *
     * @param params 参数
     * @return 活动列表
     */
    @Override
    public List<Activity> getActivitiesByName(Map<String, Object> params) {

        return activityMapper.getActivitiesByName(params);
    }

    /**
     * 根据线索id获取活动列表
     *
     * @param clueId 线索id
     * @return 活动列表
     */
    @Override
    public List<Activity> findActivitiesByClueId(String clueId) {


        return activityMapper.findActivitiesByClueId(clueId);
    }

    /**
     * 更新备注
     *
     * @param remark 备注
     * @return 是否更新成功
     */
    @Override
    public boolean updateRemark(ActivityRemark remark) {

        int count = activityRemarkMapper.updateRemark(remark);

        return count == 1;
    }

    /**
     * 保存一个备注
     *
     * @param remark 备注
     * @return 是否保存成功
     */
    @Override
    public boolean saveRemark(ActivityRemark remark) {

        int count = activityRemarkMapper.saveRemark(remark);

        return count == 1;
    }

    /**
     * 删除备注
     *
     * @param id id
     * @return 是否成功删除
     */
    @Override
    public boolean deleteRemark(String id) {

        int count = activityRemarkMapper.deleteRemarkById(id);

        return count == 1;
    }

}
